using System;
using Clandestine.Engines.Field;
using Qyoto;

namespace Shlick
{
	using Object = Clandestine.Engines.Field.Object;
	
	public class TilesTab : QWidget
	{
		public MapDocument MapDocument { get; private set; }
		private Ui_TilesTab ui;
		private TilemapWidget tilemapWidget; 
		private LayerTreeWidget layersWidget;
		
		public TilesTab (MapDocument mapDocument)
		{
			ui = new Ui_TilesTab();
			ui.SetupUi(this);
			
			this.MapDocument = mapDocument;
			
			// Tilemap Widget
			QVBoxLayout vbox = new QVBoxLayout(this.ui.frameTilemap);
			vbox.SetContentsMargins(0, 0, 0, 0);
			tilemapWidget = new TilemapWidget(this.MapDocument);
			vbox.AddWidget(tilemapWidget);
			
			// Layers Widget
			vbox = new QVBoxLayout(this.ui.frameLayers);
			vbox.SetContentsMargins(0, 0, 0, 0);
			layersWidget = new LayerTreeWidget(this, this.MapDocument, LayerTreeMode.Map, true);
			layersWidget.ItemSelected += OnLayerTreeSelection;
			vbox.AddWidget(layersWidget);
		}
		
		// When tab is focused
		public void Activated()
		{
			OnLayerTreeSelection(layersWidget.SelectedItemType);
		}
		
		public void OnLayerTreeSelection(LayerTreeItem item)
		{
			// Type of selection
			switch (item)
			{
				case LayerTreeItem.ObjectList:
					this.MapDocument.ToolManager.SwitchToTool(ToolTypes.ObjectSelect);
					break;
				case LayerTreeItem.Layer:
				case LayerTreeItem.LayerGroup:
				case LayerTreeItem.Level:
				case LayerTreeItem.Map:
				default:
					this.MapDocument.ToolManager.SwitchToTool(ToolTypes.TilePainter);
					break;
			}
		}
		
		public void EditObject(Object o)
		{
			layersWidget.UpdateMode(LayerTreeMode.Object, o);
		}
	}
}

